package com.insigma.business.qrcode.common.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.insigma.business.common.dto.CurrentUser;
import com.insigma.business.common.utils.CurrentUserUtil;
import com.insigma.business.qrcode.common.converter.CareWelfareMerchantEntryApplyProviderConverter;
import com.insigma.business.qrcode.common.converter.CareWelfareMerchantInfoProviderConverter;
import com.insigma.business.qrcode.common.service.UserSwitchService;
import com.insigma.business.qrcode.common.template.area.BaseProviderImpl;
import com.insigma.business.qrcode.common.template.area.ScenarioCodeEnum;
import com.insigma.business.qrcode.common.template.area.ServiceCallback;
import com.insigma.business.qrcode.common.util.BizCheckUtil;
import com.insigma.business.qrcode.common.util.ResponseUtils;
import com.insigma.business.qrcode.common.vo.BaseResponse;
import com.insigma.business.qrcode.common.vo.UserTypeEnum;
import com.insigma.business.qrcode.merchant.dao.CareWelfareMerchantEntryApplyDao;
import com.insigma.business.qrcode.merchant.dto.CareWelfareMerchantEntryApplyDTO;
import com.insigma.business.qrcode.merchant.dto.OutStatusEnum;
import com.insigma.business.qrcode.merchant.dto.request.CareWelfareMerchantEntryApplySearchRequest;
import com.insigma.business.qrcode.merchant.dto.request.CareWelfareMerchantInfoSearchRequest;
import com.insigma.business.qrcode.merchant.entity.CareWelfareMerchantEntryApply;
import com.insigma.business.qrcode.merchant.entity.CareWelfareMerchantInfoDO;
import com.insigma.business.qrcode.merchant.service.CareWelfareMerchantInfoService;
import com.insigma.business.qrcode.welfare.dto.CareWelfareMerchantInfoDTO;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class UserSwitchServiceImpl extends BaseProviderImpl implements UserSwitchService {

    @Resource
    private CareWelfareMerchantInfoService careWelfareMerchantInfoService;

    @Resource
    private CareWelfareMerchantEntryApplyDao careWelfareMerchantEntryApplyDao;

    @Override
    public BaseResponse<List<JSONObject>> getUserType(CareWelfareMerchantInfoSearchRequest careWelfareMerchantInfoSearchRequest) {
        BaseResponse<List<JSONObject>> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(baseResponse, new ServiceCallback() {

            @Override
            public void check() {
                BizCheckUtil.checkNotBlank(careWelfareMerchantInfoSearchRequest.getOuterSysAccountId(),"openid不能为空！");
            }

            @Override
            public void executeService() {
                List<JSONObject> userTypes = new ArrayList<>();
                CurrentUser currentUser = CurrentUserUtil.getCurrentUser();
                JSONObject userJs = new JSONObject();
                userJs.put("userType",currentUser.getLoginUserType());
                userTypes.add(userJs);
                List<CareWelfareMerchantInfoDO> careWelfareMerchantInfoDOList = careWelfareMerchantInfoService.getByOuterSysId(careWelfareMerchantInfoSearchRequest.getOuterSysAccountId());
                List<CareWelfareMerchantEntryApply> careWelfareMerchantEntryApplyList = careWelfareMerchantEntryApplyDao.getByOuterSysId(careWelfareMerchantInfoSearchRequest.getOuterSysAccountId());
                if (!CollectionUtils.isEmpty(careWelfareMerchantEntryApplyList)) {
                    Optional<CareWelfareMerchantEntryApply> x = careWelfareMerchantEntryApplyList.stream().max(Comparator.comparing(CareWelfareMerchantEntryApply::getModifyDatetime));
                    CareWelfareMerchantEntryApply recent = x.get();
                    if (CollectionUtils.isEmpty(careWelfareMerchantInfoDOList) ) {
                        if ("WAIT".equals(recent.getStatus())) {
                            JSONObject merchantJs = new JSONObject();
                            merchantJs.put("merchantType",UserTypeEnum.MERCHANT_USER_WAIT.name());
                            userTypes.add(merchantJs);
                        } else if ("REJECT".equals(recent.getStatus())){
                            JSONObject merchantJs = new JSONObject();
                            merchantJs.put("merchantType",UserTypeEnum.MERCHANT_USER_REJECT.name());
                            merchantJs.put("merchantRejectMsg",recent.getStatusRemark());
                            userTypes.add(merchantJs);
                        }
                    } else {
                        for (CareWelfareMerchantInfoDO careWelfareMerchantInfoDO : careWelfareMerchantInfoDOList) {
                            if (OutStatusEnum.IN.name().equals(careWelfareMerchantInfoDO.getOutStatus())) {
                                JSONObject merchantJs = new JSONObject();
                                merchantJs.put("merchantType",UserTypeEnum.MERCHANT_USER.name());
                                merchantJs.put("merchantId",careWelfareMerchantInfoDO.getId());
                                merchantJs.put("merchantName",careWelfareMerchantInfoDO.getName());
                                userTypes.add(merchantJs);
                                break;
                            } else if (OutStatusEnum.OUT_WAIT.name().equals(careWelfareMerchantInfoDO.getOutStatus())) {
                                JSONObject merchantJs = new JSONObject();
                                merchantJs.put("merchantType",UserTypeEnum.MERCHANT_USER_OUT_WAIT.name());
                                merchantJs.put("merchantId",careWelfareMerchantInfoDO.getId());
                                merchantJs.put("merchantName",careWelfareMerchantInfoDO.getName());
                                userTypes.add(merchantJs);
                            } else if (OutStatusEnum.QXRZ.name().equals(careWelfareMerchantInfoDO.getOutStatus()) || OutStatusEnum.ZDTC.name().equals(careWelfareMerchantInfoDO.getOutStatus())) {
                                if ("WAIT".equals(recent.getStatus())) {
                                    JSONObject merchantJs = new JSONObject();
                                    merchantJs.put("merchantType",UserTypeEnum.MERCHANT_USER_WAIT.name());
                                    merchantJs.put("merchantId",careWelfareMerchantInfoDO.getId());
                                    merchantJs.put("merchantName",careWelfareMerchantInfoDO.getName());
                                    userTypes.add(merchantJs);
                                } else if ("REJECT".equals(recent.getStatus())){
                                    JSONObject merchantJs = new JSONObject();
                                    merchantJs.put("merchantType",UserTypeEnum.MERCHANT_USER_REJECT.name());
                                    merchantJs.put("merchantId",careWelfareMerchantInfoDO.getId());
                                    merchantJs.put("merchantName",careWelfareMerchantInfoDO.getName());
                                    merchantJs.put("merchantRejectMsg",recent.getStatusRemark());
                                    userTypes.add(merchantJs);
                                } else {
                                    JSONObject merchantJs = new JSONObject();
                                    merchantJs.put("merchantType",UserTypeEnum.MERCHANT_USER_OUT.name());
                                    merchantJs.put("merchantId",careWelfareMerchantInfoDO.getId());
                                    merchantJs.put("merchantName",careWelfareMerchantInfoDO.getName());
                                    userTypes.add(merchantJs);
                                }
                            }

                        }
                    }
                } else {
                    for (CareWelfareMerchantInfoDO careWelfareMerchantInfoDO : careWelfareMerchantInfoDOList) {
                        if (OutStatusEnum.IN.name().equals(careWelfareMerchantInfoDO.getOutStatus())) {
                            JSONObject merchantJs = new JSONObject();
                            merchantJs.put("merchantType",UserTypeEnum.MERCHANT_USER.name());
                            merchantJs.put("merchantId",careWelfareMerchantInfoDO.getId());
                            merchantJs.put("merchantName",careWelfareMerchantInfoDO.getName());
                            userTypes.add(merchantJs);
                            break;
                        } else if (OutStatusEnum.OUT_WAIT.name().equals(careWelfareMerchantInfoDO.getOutStatus())) {
                            JSONObject merchantJs = new JSONObject();
                            merchantJs.put("merchantType",UserTypeEnum.MERCHANT_USER_OUT_WAIT.name());
                            merchantJs.put("merchantId",careWelfareMerchantInfoDO.getId());
                            merchantJs.put("merchantName",careWelfareMerchantInfoDO.getName());
                            userTypes.add(merchantJs);
                        } else if (OutStatusEnum.QXRZ.name().equals(careWelfareMerchantInfoDO.getOutStatus()) || OutStatusEnum.ZDTC.name().equals(careWelfareMerchantInfoDO.getOutStatus())) {
                            JSONObject merchantJs = new JSONObject();
                            merchantJs.put("merchantType",UserTypeEnum.MERCHANT_USER_OUT.name());
                            merchantJs.put("merchantId",careWelfareMerchantInfoDO.getId());
                            merchantJs.put("merchantName",careWelfareMerchantInfoDO.getName());
                            userTypes.add(merchantJs);
                        }

                    }
                }

                ResponseUtils.setupSuccessResponse(baseResponse, userTypes);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.CARE_WELFARE_MERCHANT_INFO_UPDATE;
            }

        });

        return baseResponse;
    }

    @Override
    public BaseResponse<CareWelfareMerchantInfoDTO> switchMerchant(CareWelfareMerchantInfoSearchRequest careWelfareMerchantInfoSearchRequest) {
        BaseResponse<CareWelfareMerchantInfoDTO> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(baseResponse, new ServiceCallback() {

            @Override
            public void check() {
                BizCheckUtil.checkNotBlank(careWelfareMerchantInfoSearchRequest.getOuterSysAccountId(),"openid不能为空！");
            }

            @Override
            public void executeService() {
                List<CareWelfareMerchantInfoDO> careWelfareMerchantInfoDOList = careWelfareMerchantInfoService.getByOuterSysId(careWelfareMerchantInfoSearchRequest.getOuterSysAccountId());
                CareWelfareMerchantInfoDTO careWelfareMerchantInfoDTO = null;
                if (!CollectionUtils.isEmpty(careWelfareMerchantInfoDOList)) {
                    Optional<CareWelfareMerchantInfoDO> x = careWelfareMerchantInfoDOList.stream().max(Comparator.comparing(CareWelfareMerchantInfoDO::getModifyDatetime));
                    CareWelfareMerchantInfoDO careWelfareMerchantInfoDO = x.get();
                    careWelfareMerchantInfoDTO = CareWelfareMerchantInfoProviderConverter.convertResponseDO2DTO(careWelfareMerchantInfoDO);
                }
                ResponseUtils.setupSuccessResponse(baseResponse, careWelfareMerchantInfoDTO);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.CARE_WELFARE_MERCHANT_INFO_UPDATE;
            }

        });

        return baseResponse;
    }

    @Override
    public BaseResponse<CareWelfareMerchantEntryApplyDTO> getMerchantApply(CareWelfareMerchantEntryApplySearchRequest careWelfareMerchantEntryApplySearchRequest) {
        BaseResponse<CareWelfareMerchantEntryApplyDTO> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(baseResponse, new ServiceCallback() {

            @Override
            public void check() {
                BizCheckUtil.checkNotBlank(careWelfareMerchantEntryApplySearchRequest.getOuterSysAccountId(),"openid不能为空！");
            }

            @Override
            public void executeService() {
                List<CareWelfareMerchantEntryApply> careWelfareMerchantEntryApplyList = careWelfareMerchantEntryApplyDao.getByOuterSysId(careWelfareMerchantEntryApplySearchRequest.getOuterSysAccountId());
                if (!CollectionUtils.isEmpty(careWelfareMerchantEntryApplyList)) {
                    Optional<CareWelfareMerchantEntryApply> x = careWelfareMerchantEntryApplyList.stream().max(Comparator.comparing(CareWelfareMerchantEntryApply::getModifyDatetime));
                    CareWelfareMerchantEntryApplyDTO careWelfareMerchantEntryApplyDTO = CareWelfareMerchantEntryApplyProviderConverter.convertResponseDO2DTO(x.get());
                    ResponseUtils.setupSuccessResponse(baseResponse, careWelfareMerchantEntryApplyDTO);
                } else {
                    ResponseUtils.setupSuccessResponse(baseResponse, null);
                }
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.CARE_WELFARE_MERCHANT_INFO_UPDATE;
            }

        });

        return baseResponse;
    }
}
